package com.xzj.common.okhttp.util;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSONObject;
import okhttp3.Headers;
import okhttp3.MediaType;
import okhttp3.Request;
import okhttp3.RequestBody;
import okio.Buffer;

import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

/**
 * @program: xzj-common
 * @ClassName: HttpCommonUtil
 * @description: 公用方法
 * @author: XiaZunJun
 * @create: 2024-07-23 14:08
 */
public class HttpCommonUtil {

    /**
     * 根据map获取get请求参数
     *
     * @param queries : get请求的入参数据
     * @return StringBuffer
     */
    public static StringBuffer getQueryString(String url, Map<String, String> queries) {
        StringBuffer sb = new StringBuffer(url);
        if (queries != null && !queries.keySet().isEmpty()) {
            boolean firstFlag = true;
            for (String key : queries.keySet()) {
                if (firstFlag) {
                    sb.append("?").append(key).append("=").append(queries.get(key));
                    firstFlag = false;
                } else {
                    sb.append("&").append(key).append("=").append(queries.get(key));
                }
            }
        }
        return sb;
    }

    /**
     * 根据map获取get请求参数
     *
     * @param url     请求的url
     * @param queries 请求的参数，在浏览器？后面的数据，没有可以传null
     * @return StringBuffer
     */
    public static StringBuffer getQueryString(String url, JSONObject queries) {
        StringBuffer sb = new StringBuffer(url);
        if (queries != null && !queries.keySet().isEmpty()) {
            boolean firstFlag = true;
            for (String key : queries.keySet()) {
                if (firstFlag) {
                    sb.append("?").append(key).append("=").append(queries.getString(key));
                    firstFlag = false;
                } else {
                    sb.append("&").append(key).append("=").append(queries.getString(key));
                }
            }
        }
        return sb;
    }

    public static String analysisRequestBody(RequestBody requestBody) {
        if (null != requestBody) {
            Buffer buffer = new Buffer();
            try {
                requestBody.writeTo(buffer);
            } catch (IOException e) {
                return "";
            }
            //编码设为UTF-8
            Charset charset = StandardCharsets.UTF_8;
            MediaType contentType = requestBody.contentType();
            if (contentType != null) {
                charset = contentType.charset(StandardCharsets.UTF_8);
            }
            if (charset != null) {
                //拿到request
                return buffer.readString(charset);
            }
        }
        return "";
    }

    public static Map<String, String> convertHeader(String header) {
        JSONObject headerJson = JSONObject.parseObject(header);
        Map<String, String> headerMap = new HashMap<>();
        for (String key : headerJson.keySet()) {
            headerMap.put(key, headerJson.getString(key));
        }
        return headerMap;
    }

    public static Request judgeHeaders(String url, RequestBody requestBody, String header) {
        Request request;
        if (StrUtil.isNotEmpty(header)) {
            Map<String, String> headerMap = HttpCommonUtil.convertHeader(header);
            ;
            request = new Request.Builder().url(url).post(requestBody).headers(Headers.of(headerMap)).build();
        } else {
            request = new Request.Builder().url(url).post(requestBody).build();
        }
        return request;
    }
}
